#include"apue.h"
#include"ftp.h"
//命令处理函数 CD
void cli_cd(int sockfd,struct sockaddr_in *addr,struct cli_to_ser *cmd_struct)
{	
	struct sockaddr_in ser_addr;
	struct ser_to_cli res_ult;
	int addrlen=sizeof(struct sockaddr_in);

	bzero(&ser_addr,sizeof(ser_addr));
	bzero(&res_ult,sizeof(struct ser_to_cli));
	sendto(sockfd,cmd_struct,sizeof(struct cli_to_ser),0,
			(struct sockaddr *)addr,sizeof(struct sockaddr_in));
	recvfrom(sockfd,&res_ult,sizeof(struct ser_to_cli),0,(struct sockaddr*)&ser_addr,&addrlen);
	
	if(res_ult.result==SUCCESS)
	{
		printf("cd SUCCESS!\n");
	}else
	{
		printf("cd 失败!\n");
	}

}
//命令处理函数 PWD
void cli_pwd(int sockfd,struct sockaddr_in *addr,struct cli_to_ser *cmd_struct)
{	
	struct ser_to_cli res_ult;
	struct sockaddr_in ser_addr;
	int addrlen=sizeof(struct sockaddr_in);

	bzero(&ser_addr,sizeof(ser_addr));
	bzero(&res_ult,sizeof(struct ser_to_cli));
	sendto(sockfd,cmd_struct,sizeof(struct cli_to_ser),0,
			(struct sockaddr *)addr,sizeof(struct sockaddr_in));
	perror("sendto");
	recvfrom(sockfd,&res_ult,sizeof(struct ser_to_cli),0,
			(struct sockaddr*)&ser_addr,&addrlen);
	if(res_ult.result==SUCCESS)
	{
		printf("SUCCESS!\n");
		printf("路径=%s\n",res_ult.string_result);
	}else
	{
		printf("Fail失败!\n");
	}
}
//命令处理函数 LS
void cli_ls(int sockfd,struct sockaddr_in *addr,struct cli_to_ser *cmd_struct)
{	
	struct ser_to_cli res_ult;
	struct sockaddr_in ser_addr;
	int addrlen=sizeof(struct sockaddr_in);

	sendto(sockfd,cmd_struct,sizeof(struct cli_to_ser),0,
			(struct sockaddr *)addr,sizeof(struct sockaddr_in));
	while(1)
	{
		bzero(&ser_addr,sizeof(ser_addr));
		bzero(&res_ult,sizeof(struct ser_to_cli));
		recvfrom(sockfd,&res_ult,sizeof(struct ser_to_cli),0,(struct sockaddr*)&ser_addr,&addrlen);
		if(res_ult.result==SUCCESS)
		{
			printf("%s\n",res_ult.string_result);
		}else
		{
			if(res_ult.result==LS_OVER)
			{
				printf("ls sucess!\n");
				break;
			}else
			{
				printf("ls fail!\n");
			}
		}
	}
}
//命令处理函数 GET
void cli_get(int sockfd,struct sockaddr_in *addr,struct cli_to_ser *cmd_struct)
{	
	struct ser_to_cli res_ult;
	struct sockaddr_in ser_addr;
	int addrlen=sizeof(struct sockaddr_in);
//将命令发送到服务器
	bzero(&ser_addr,sizeof(ser_addr));
	bzero(&res_ult,sizeof(struct ser_to_cli));
	sendto(sockfd,cmd_struct,sizeof(struct cli_to_ser),0,
			(struct sockaddr *)addr,sizeof(struct sockaddr_in));
//进行tcp连接进行文件的下载
	struct sockaddr_in tcp_addr;
	bzero(&tcp_addr,sizeof(tcp_addr));
	
	int sock_tcp,confd;
	int addrlen_tcp=sizeof(struct sockaddr_in);
	int ret,num;
	char buf[BUFSIZ];
	sock_tcp=socket(AF_INET,SOCK_STREAM,0);
	if(sock_tcp<0)
	{
		perror("socket");
		printf("下载失败！\n");
		return;
	}
	tcp_addr.sin_family=AF_INET;
	tcp_addr.sin_port=htons(TCP_PORT);
	tcp_addr.sin_addr.s_addr=htonl(INADDR_ANY);

	ret=bind(sock_tcp,(struct sockaddr *)&tcp_addr,sizeof(struct sockaddr_in));
	if(ret<0)
	{
		perror("bind sock_tcp");
		printf("下载失败！\n");
		close(sock_tcp);
		return;
	}
	ret=listen(sock_tcp,5);
	if(ret<0)
	{
		perror("listen");
		printf("下载失败！\n");
		close(sock_tcp);
		return;
	}
	bzero(&tcp_addr,sizeof(tcp_addr));
	confd=accept(sock_tcp,(struct sockaddr *)&tcp_addr,&addrlen_tcp);
	if(confd<0)
	{
		perror("accept");
		printf("下载失败！\n");
		close(sock_tcp);
		return;
	}
	int fd;
	fd=open(cmd_struct->ftp_arg.argPut,O_CREAT|O_WRONLY);
	if(fd<0)
	{
		perror("open");
		printf("下载失败！\n");
		close(confd);
		close(sock_tcp);
		return;
	}
	while(1)
	{
		num=read(confd,buf,sizeof(buf));
		write(fd,buf,num);
		if(num==0) break;
	}
	
	close(fd);
	close(sock_tcp);
	recvfrom(sockfd,&res_ult,sizeof(struct ser_to_cli),0,(struct sockaddr*)&ser_addr,&addrlen);
	if(res_ult.result==SUCCESS)
	{		
		printf("下载完成!\n");
	}else
	{
		
		printf("下载失败！\n");
	}
}
//命令处理函数 PUT
void cli_put(int sockfd,struct sockaddr_in *addr,struct cli_to_ser *cmd_struct)
{	
	struct ser_to_cli res_ult;
	struct sockaddr_in ser_addr;
	struct sockaddr_in tcp_addr;
	int sock_tcp,ret;
	int fd,num;
	char buf[BUFSIZ];

	bzero(&res_ult,sizeof(struct ser_to_cli));

	
	sendto(sockfd,cmd_struct,sizeof(struct cli_to_ser),0,
			(struct sockaddr *)addr,sizeof(struct sockaddr_in));
	perror("sendto");
	fd=open(cmd_struct->ftp_arg.argPut,O_RDONLY);
	if(fd<0)
	{
		perror("open");
		return;
	}
	printf("打开文件成功，准备建立网络连接\n");
	bzero(&ser_addr,sizeof(ser_addr));
	int addrlen=sizeof(struct sockaddr_in);
	recvfrom(sockfd,&res_ult,sizeof(struct ser_to_cli),0,(struct sockaddr*)&ser_addr,&addrlen);
	if(res_ult.result!=666) return;
	sock_tcp=socket(AF_INET,SOCK_STREAM,0);
	if(sock_tcp<0)
	{
		perror("sockfd");
		close(fd);
		return;
	}
	bzero(&tcp_addr,sizeof(tcp_addr));
	tcp_addr=*addr;
	tcp_addr.sin_port=htons(TCP_PUT);
	ret=connect(sock_tcp,(struct sockaddr *)&tcp_addr,sizeof(struct sockaddr_in));
	if(ret<0)
	{
		perror("connect");
		close(fd);
		close(sock_tcp);
		return;
	}
	printf("网络链接建立成功，准备读写数据\n");
	while(1)
	{
		num=read(fd,buf,sizeof(buf));
		perror("read");
		write(sock_tcp,buf,sizeof(buf));
		perror("write");
		if(num==0) break;

	}
	printf("读写完成\n");
	close(sock_tcp);
	close(fd);
	bzero(&ser_addr,sizeof(ser_addr));
	recvfrom(sockfd,&res_ult,sizeof(struct ser_to_cli),0,(struct sockaddr*)&ser_addr,&addrlen);
	if(res_ult.result==SUCCESS)
	{
		printf("上传成功\n");
	}else
		if(res_ult.result==FAIL)
		{
			printf("上传失败\n");
		}
	printf("quit\n");
}
//命令处理函数 QUIT
int cli_quit(int sockfd,struct sockaddr_in *addr,struct cli_to_ser *cmd_struct)
{	
	struct ser_to_cli res_ult;
	struct sockaddr_in ser_addr;
	int addrlen=sizeof(struct sockaddr_in);

	bzero(&ser_addr,sizeof(ser_addr));
	bzero(&res_ult,sizeof(struct ser_to_cli));
	sendto(sockfd,cmd_struct,sizeof(struct cli_to_ser),0,
			(struct sockaddr *)addr,sizeof(struct sockaddr_in));
	perror("sendto");
	recvfrom(sockfd,&res_ult,sizeof(struct ser_to_cli),0,
			(struct sockaddr*)&ser_addr,&addrlen);
	return res_ult.result;

}


void judge_cmd(char *pcmd_str,struct cli_to_ser *cmd_struct)
{	
	int ret;

	strtok(pcmd_str," ");
	ret=strcmp(pcmd_str,FTP_PWD);
	if(ret==0)
	{
		cmd_struct->ftm_cmd=CMD_PWD;
		goto ERR;
	}
	ret=strcmp(pcmd_str,FTP_LS);
	if(ret==0)
	{
		cmd_struct->ftm_cmd=CMD_LS;
		goto ERR;
	}
	ret=strcmp(pcmd_str,FTP_CD);
	if(ret==0)
	{
		cmd_struct->ftm_cmd=CMD_CD;
		goto ERR;
	}
	ret=strcmp(pcmd_str,FTP_QUIT);
	if(ret==0)
	{
		cmd_struct->ftm_cmd=CMD_QUIT;
		goto ERR;
	}
	ret=strcmp(pcmd_str,FTP_GET);
	if(ret==0)
	{
		cmd_struct->ftm_cmd=CMD_GET;
		goto ERR;
	}
	ret=strcmp(pcmd_str,FTP_PUT);
	if(ret==0)
	{
		cmd_struct->ftm_cmd=CMD_PUT;
		goto ERR;
	}
	cmd_struct->ftm_cmd=CMD_MAX;
ERR:
	while(pcmd_str=strtok(NULL," "))
	{
		strcpy(cmd_struct->ftp_arg.arg,pcmd_str);
	}
}
